const mysql2 = require('mysql2');

const pool = mysql2.createPool({
  host: '175.178.56.205',
    user: 'root',
    password: 'Cu159753!cU',
    database: 'beauty_system',
    waitForConnections: true,
    connectionLimit: 10,
    idleTimeout: 60000,
    queueLimit: 0
})


exports.mysqlConnect = function () {
  // try {
  //   // 创建连接
  //   const connection = await pool.getConnection()

  //   return connection;
  // }
  // catch (error) {
  //   console.log('连接失败', error);
  // }
}

exports.setPoolConnection = async function(req, res, next) {
    try {
      const connection = await pool.promise().getConnection();
      // console.log(connection, '连接')
      if( !connection) {
        return res.resend.sendJson(req,res, {
          status: 500,
          code: 'FAIL',
          data: '',
          msg: '数据库连接失败' 
        })
      }
      req.connection = connection;
      return next();
    }
    catch (error) {
      return res.resend.sendJson(req,res, {
        status: 500,
        code: 'FAIL',
        data: '',
        msg: error.message,
        logMsg: error
      })
    }
};